package cn.mw.cmdb.config;

import cn.mw.log.client.LogClientSimple;
import cn.mw.log.enums.BusinessLogType;
import cn.mw.log.enums.LogLevel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

/**
 * 记录系统日志
 */
@Slf4j
@Component
public class CMDBLogManager implements CommandLineRunner, DisposableBean {

    @Autowired
    private LogClientSimple logClientSimple;

    @Autowired
    private MessageSource messageSource;;

    @Override
    public void run(String... args) throws Exception {
        log.info("服务启动, 记录启动日志");
        logClientSimple.addSystemLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(), messageSource.getMessage("cmdb-server-start", null, LocaleContextHolder.getLocale()));
    }

    @Override
    public void destroy() throws Exception {
        log.info("服务启动, 记录关闭日志");
        logClientSimple.addSystemLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(), messageSource.getMessage("cmdb-server-stop", null, LocaleContextHolder.getLocale()));

    }
}
